package com.dalin.platform.pub.filters;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dalin.common.PropertyConst;
import com.dalin.platform.loa.vo.WsCurrentUserInfo;

public class LoaFilter implements Filter {
	private FilterConfig filterConfig;   

	public void init(FilterConfig filterConfig) throws ServletException {   
        this.filterConfig = filterConfig;   
    }   

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain fc) {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		try {
			Object currentUser = req.getSession().getAttribute(PropertyConst.USER_SESSION_NAME);
			String url = req.getRequestURI().substring(
					req.getContextPath().length());
			if ((url.endsWith("/") || url.endsWith(".jsp")) && !url.endsWith("login.jsp")) {
				if (currentUser == null) {
					res.sendRedirect(req.getContextPath() + "/loa/login.html");
					return;
				}
			}
			WsCurrentUserInfo user = (WsCurrentUserInfo)currentUser;
		//	System.out.println("url============="+url);
//			if (user != null && !PlatContext.getWsSecurityService()
//					.checkURI(user.getLum(), url)) {
//				res.setCharacterEncoding("UTF-8");
//				res.setContentType("text/html;charset=UTF-8");
//				res.getWriter().print("{success:false, info: '对不起，你无权限访问此资源路径'}");
//				return;
//			}
			fc.doFilter(req, res);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void destroy() {
		
	}
}
